#include "baselist.h"

/*
int wxCALLBACK ListCompare(long item1, long item2, long WXUNUSED(sortData))
{
    // inverse the order
    if (item1 < item2)
        return -1;
    if (item1 > item2)
        return 1;

    return 0;
}
*/

BaseList::BaseList( wxListCtrl* pcListCtrl, wxString& soundFileDir )
{
	m_pcListCtrl = pcListCtrl;
	m_lItemIdx = -1;
	m_sSoundDir = soundFileDir;
	
	m_bToggle = false;

	m_lItemNum = 0;

	// connect sorting event to the list
	//this->BaseList::Connect( m_pcListCtrl->GetId(), wxID_ANY, wxCommandEventHandler( BaseList::OnSort ) );
}
	
BaseList::~BaseList()
{
	// disconnect sorting event to the list
	//this->BaseList::Disconnect( m_pcListCtrl->GetId(), wxID_ANY, wxCommandEventHandler( BaseList::OnSort ) );
}

void BaseList::CheckValidity( wxString& str )
{
	str.Replace( wxT("\\"), wxT("") );
	str.Replace( wxT("\""), wxT("\"\"") );
	str.Replace( wxT("'"), wxT("''") );
}

wxString BaseList::GetValidString( wxString& str )
{
	wxString temp = str;
	temp.Replace( wxT("\\"), wxT("") );
	temp.Replace( wxT("\""), wxT("\"\"") );
	temp.Replace( wxT("'"), wxT("''") );
	
	return temp;
}


long BaseList::TableToLong( wxString& str, size_t startPos )
{
	long lNum = 0;
	wxString s = str.Mid( startPos );
	s.ToLong( &lNum );
	
	return lNum;
}

void BaseList::PlayFile( wxString& file )
{
	if( file.empty() )
		return;
		
	Mix_OpenAudio( 44100, AUDIO_S16SYS, 2, 2048 );
	Mix_Chunk *sound = Mix_LoadWAV( file.mb_str() );
	Mix_PlayChannel( -1, sound, 0 );
	//sleep(1);
	Mix_CloseAudio();
}

wxString BaseList::GetColumnText( long colIndex )
{
	if( m_lItemIdx < 0 || colIndex < 0 )
		return _("");
		
	wxListItem info;
	info.SetId( m_lItemIdx );
	info.SetColumn( colIndex );
	info.SetMask( wxLIST_MASK_TEXT );
	m_pcListCtrl->GetItem( info );		

	return info.GetText();
}

wxString BaseList::GetColumnText( long itemIdx, long colIndex )
{
	if( itemIdx < 0 || colIndex < 0 )
		return _("");
		
	wxListItem info;
	info.SetId( itemIdx );	
	info.SetColumn( colIndex );
	info.SetMask( wxLIST_MASK_TEXT );
	m_pcListCtrl->GetItem( info );		

	return info.GetText();
}

void BaseList::InsertItem( wxString* pString, long end )
{
	long idx = m_pcListCtrl->GetItemCount();
	
	long tmp = m_pcListCtrl->InsertItem( idx, pString[0] );
	//SetItemData(tmp, pString[0][0]);
	if( tmp >= 0 )
		for( int i = 1; i < end; i++ )
			m_pcListCtrl->SetItem( tmp, i, pString[i] );
}

wxString BaseList::GetFileName( wxString& wordId )
{
	wxDir dir( m_sSoundDir );	
	wxString file;
	if( dir.IsOpened() )
	{
		wxString spec = wordId + wxT(".*");
		dir.GetFirst( &file, spec );	
	}
	
	if( !file.empty() )
		file = m_sSoundDir + wxT("/") + file;
	
	return file;
}

wxString BaseList::GetFileName( wxString& path, wxString& wordId )
{
	wxDir dir( path );	
	wxString file;
	if( dir.IsOpened() )
	{
		wxString spec = wordId + wxT(".*");
		dir.GetFirst( &file, spec );	
	}
	
	if( !file.empty() )
		file = path + wxT("/") + file;
	
	return file;
}

/*
void BaseList::OnSort( wxCommandEvent& event )
{
	m_pcListCtrl->SortItems( ListCompare, 0 );
}
*/